package com.kkb.interceptor;

import com.kkb.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

/**
 *
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String requestURI = request.getRequestURI();
        //System.out.println("URI:" + requestURI);
        if (user == null) {
           String requestURI1 = requestURI.toLowerCase();
            if (requestURI.endsWith("index.html") || requestURI1.startsWith("/user")||requestURI1.startsWith("/role")||requestURI1.startsWith("/resource")) {
                response.setContentType("text/html;charset=utf-8");
                PrintWriter writer = response.getWriter();
                writer.print("<script>top.location.href='/login.html'</script>");
                return false;
            }
        }
        return true;
    }
}
